<?php

namespace app\admin\service;

use app\common\model\commonModel;
use app\common\model\trResource;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * banner服务类
 */
class BannerService
{
    private $returnData;
    private $field = [
        "title" => "", "icon" => "require", "image_size" => '', 'url' => ''
    ]; //填入数据库需要存储的表字段
    private $data = [];
    private $mall_id = 0;
    private $params;

    public function __construct($data, $params = [])
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    public function transferField($data){
        if(!$data){
            return [];
        }
        if(isset($data[0])){
            foreach ($data as $k => $item){
                if(isset($item["icon"])){
                    $data[$k]["iconUrl"] = tome_dia(trResource::getValue("url", ["id" => $item["icon"]]));
                }
            }
        }else{
            if(isset($data["icon"])) {
                $data["iconUrl"] = tome_dia(trResource::getValue("url", ["id" => $data["icon"]]));
            }
        }
        return $data;
    }

    // 列表
    public function getList(){
        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pageSize = isset($this->params['pageSize']) ? $this->params['pageSize'] : 10;
        //搜索条件
        $filter = array(["mall_id", "=", $this->mall_id]);
        if(isset($this->params['title'])){
            $filter[] = ['title', 'like', $this->params["title"] . "%"];
        }
        $data = commonModel::getList(bannerTable, "*", $filter, $page, $pageSize);
        if(!$data["data"]){
            $this->returnData['data'] = ['list' => [], 'totalPage' => $page, 'currentPage' => $page, 'count' => 0];
        }else {
            $data['data'] = $this->transferField($data['data']);
            $this->returnData['data'] = ['list' => $data['data'], 'totalPage' => $data['last_page'], 'currentPage' => $data['current_page'], 'count' => $data['total']];
        }
        return $this->returnData;
    }
    
    // 添加banner
    public function toAdd(){
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                return ErrorCode::code("params_not_exist", $ks);
            }
            $this->data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        $this->data["mall_id"] = $this->mall_id;
        $imageData = getimagesize(tome_dia(trResource::getValue("url", ["id" => $this->data["icon"]])));
        $this->data["image_size"] = "{$imageData[0]}px * {$imageData[1]}px";
        $this->data["create_time"] = time();
        $result = commonModel::toAdd(bannerTable, $this->data);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }
    
    public function toUpdate(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                return ErrorCode::code("params_not_exist", $ks);
            }
            if(isset($this->params[$ks])){
                $this->data[$k] = $this->params[$ks];
            }
        }
        if(isset($this->data["icon"])) {
            $imageData = getimagesize(tome_dia(trResource::getValue("url", ["id" => $this->data["icon"]])));
            $this->data["image_size"] = "{$imageData[0]}px * {$imageData[1]}px";
        }
        $result = commonModel::toUpdate(bannerTable, $this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }
    
    public function getDetail(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $data = commonModel::getFields(bannerTable, "*", ["id" => $this->params["id"], "mall_id" => $this->mall_id], true, false);
        $this->returnData["data"] = $this->transferField($data);
        return $this->returnData;
    }

    public function toChangeStatus(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        if(empty($this->params['status'])){
            return ErrorCode::code("params_not_exist", "status");
        }
        if(!in_array($this->params["status"], [2,1])){ // 1展示， 0隐藏
            return ErrorCode::code("params_error", "status");
        }
        $this->data["status"] = $this->params["status"];
        $result = commonModel::toUpdate(bannerTable, $this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    public function toDel(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $result = commonModel::delById(bannerTable, $this->params['id']);
        if (isset($result["code"])) {
            return $result;
        }
        if (!$result) {
            $this->returnData = ErrorCode::$errorCode['fail'];
            $this->returnData["msg"] = "删除失败";
        }
        return $this->returnData;
    }
}